package com.epfresh.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.epfresh.R;
import com.epfresh.activity.CommentNewActivity;
import com.epfresh.activity.H5OrderDetailActivity;
import com.epfresh.activity.HomeActivity;
import com.epfresh.activity.OrderLogisticsInfoActivity;
import com.epfresh.activity.WebActivity;
import com.epfresh.adapter.OrderAdapter;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.RequestEntityPurchase;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.entity.TagPager;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.api.widget.ptr.PtrDefaultHandler;
import com.epfresh.api.widget.ptr.PtrFrameLayout;
import com.epfresh.api.widget.ptr.PtrHandler;
import com.epfresh.bean.ContentEntity;
import com.epfresh.bean.OrderListBean;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseFragment;
import com.epfresh.global.DataManager;
import com.epfresh.wxapi.WXPayEntryActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;

/**
 * Created by wzh on 2016/5/4.
 */
@SuppressLint("ValidFragment")
public class OrdersListFragment extends BaseFragment implements LoadMoreListView.IXListViewListener {
    protected View mainView;
    protected View vException;
    protected TextView tvExceptionDescription;
    protected ImageView ivExceptionIcon;
    private ProgressBar pb;
    private int requestSucceedCount = 0;
    private int PAGE_SIZE = 5;
    private TagPager tagPager = new TagPager();
    private LoadMoreListView lvContent;
    private List<OrderListBean> list = new ArrayList<>();
    OrderAdapter adapter;

    Context context;
    int type;//订单列表类别 1,待付款 2,未完成,3已完成    0 4全部订单 9:评价

    public OrdersListFragment() {
    }

    public OrdersListFragment(int newsType) {
        if (newsType == 4) {
            this.type = 9;
            return;
        }
        this.type = newsType;
    }

    @Override
    public String getBaseTag() {
        return "OrdersListFragment";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_order_list, container, false);
        context = this.getActivity();
        initView(view);
        adapter = new OrderAdapter(context, lvContent, list);
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int position, long l) {

// 交货码，查看物流，取消订单，确认收货，支付， 详情
                switch (v.getId()) {
                    case R.id.tv_code:
                        showCodeDialog(list.get(position).getInspectCode());
                        break;
                    case R.id.tv_sure:
                        if (isWindowLocked())
                            return;
                        else
                            mHttpCheck(position);
                        break;
                    case R.id.tv_logistics:
                        Intent h5IntentTrace = new Intent(context,WebActivity.class);
                        h5IntentTrace.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_ORDER_TRACE + "orderId=" + list.get(position).getOrderId());
                        h5IntentTrace.putExtra(Constant.KEY_WEB_TITLE,"");
                        startActivity(h5IntentTrace);

//                        Intent logisticsInfo = new Intent(context, OrderLogisticsInfoActivity.class);
//                        logisticsInfo.putExtra("logisticsNo", list.get(position).getOrderId());
//                        context.startActivity(logisticsInfo);
                        break;
                    case R.id.tv_pay:
                        if (isWindowLocked())
                            return;
                        else {
                            if (list.get(position).getButtonType() == 1) {
                                Intent pay = new Intent(context, WXPayEntryActivity.class);
                                pay.putExtra("payWay", 1);
                                String orderId = list.get(position).getOrderId();
                                pay.putExtra("orderId", orderId);
                                context.startActivity(pay);
                            } else {
                                mHttpCheck(position);
                            }
                        }
                        break;
                    case R.id.tv_cancel:
                        flagPosition = position;
                        showCancelDialog();
                        break;
                    case R.id.tv_more:
                        if (isWindowLocked())
                            return;
                        else
                            mHttpMore(position);
                        break;
                    case R.id.tv_evaluate:
                        Intent h5Intent = new Intent(context, WebActivity.class);
                        h5Intent.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_ORDER_COMMENT +
                                "orderId=" + list.get(position).getOrderId() +
                                "&accountId=" + ApplicationHelper.getInstance().getUser().getAccountId());
                        h5Intent.putExtra(Constant.KEY_WEB_TITLE, "评价");
                        startActivityForResultCommon(h5Intent,0x001);
//                        Intent evaluate = new Intent(context, CommentNewActivity.class);
//                        evaluate.putExtra("orderId",list.get(position).getOrderId());
//                        startActivityForResultCommon(evaluate,0x001);
                        break;
                    case R.id.ll_order:
                    case R.id.rl_more:
                    case R.id.ll_images:
                        Intent detail = new Intent(context, H5OrderDetailActivity.class);
                        detail.putExtra("orderId", list.get(position).getOrderId());
                        detail.putExtra(Constant.KEY_WEB_URL, "http://m.epfresh.com/help/order_detail.html?orderId=" + list.get(position).getOrderId());
                        detail.putExtra(Constant.KEY_WEB_TITLE, "订单详情");
                        detail.putExtra("page", type);
                        startActivityForResultCommon(detail, 0x001);

                        break;
                }
            }
        });

        lvContent.setAdapter(adapter);
        loadData(type, tagPager.getNumber());
        return view;
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.tv_order_not_want:
                dialog.dismiss();
                if (list == null || list.size() == 0 || list.size() <= flagPosition) {
                    T.toast("请刷新当前页面");
                    return;
                }
                if (isWindowLocked())
                    return;
                else
                    mHttpCancel(list.get(flagPosition).getOrderId(), "不想要了");
                break;
            case R.id.tv_order_wrong:
                dialog.dismiss();
                if (list == null || list.size() == 0 || list.size() <= flagPosition) {
                    T.toast("请刷新当前页面");
                    return;
                }
                if (isWindowLocked())
                    return;
                else
                    mHttpCancel(list.get(flagPosition).getOrderId(), "点错了");
                break;
            case R.id.v_order_cancel:
                dialog.dismiss();
                break;
        }
    }


    void initView(View v) {

        initHeadView(v);
        vException = v.findViewById(R.id.v_exception);
        mainView = v.findViewById(R.id.fragment_ptr_home_ptr_frame);
        initException();
        lvContent = (LoadMoreListView) v.findViewById(R.id.lv_content);
        lvContent.setPullLoadEnable(true);
        lvContent.setXListViewListener(this);
    }

    private PtrFrameLayout ptrFrameLayout;
    private PtrHandler ptrHandler = new PtrHandler() {
        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return PtrDefaultHandler.checkContentCanBePulledDown(frame, lvContent, header);
        }

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            refresh();
        }
    };

    private void initHeadView(View v) {
        ptrFrameLayout = (PtrFrameLayout) v.findViewById(R.id.fragment_ptr_home_ptr_frame);
        ptrFrameLayout.setPtrHandler(ptrHandler);
    }

    boolean refreshFlag = false;

    public void refresh() {
        lvContent.resetNoMore();
        refreshFlag = true;
        loadData(type, 0);
    }

    @Override
    public void onLoadMore() {
        int number = tagPager.getNumber();
        loadData(type, number + 1);
    }

    public void updateViewStatus(RequestTag requestTag) {
        ptrFrameLayout.refreshComplete();
        if (tagPager.isLast()) {
            lvContent.noMore();
        } else {
            lvContent.stopLoadMore();
        }
    }

    void loadData(int type, int page) {
        RequestEntityPurchase<Map<String, String>> entity = new RequestEntityPurchase<>();
        Map<String, String> map = new HashMap<>();
        if (getUser() == null || getUser().getAccountId() == null) {
            return;
        }
        map.put("accountId", getUser().getAccountId());////
        if (type != 0) {
            map.put("type", "" + type);////
        }
        map.put("pageNumber", page + "");///
        map.put("pageSize", PAGE_SIZE + "");////
        map.put("sortProperty", "placeOrderTime");
        map.put("sortDirection", "DESC");
        entity.setCmd(AppPurchaseRouterConstant.cmd_order_order_list);
        entity.setParameters(map);
        RequestTag requestTag = new RequestTag();
        requestTag.tag = AppPurchaseRouterConstant.cmd_order_order_list;
        requestTag.action = page;
        requestTag.type = RequestTag.TYPE_CURRENT;
        request(entity, requestTag, onRequestListener);
    }


    public void initException() {
        tvExceptionDescription = (TextView) vException.findViewById(R.id.tv_exception);
        ivExceptionIcon = (ImageView) vException.findViewById(R.id.iv_exception);
        pb = (ProgressBar) vException.findViewById(R.id.pb);
    }


    public void showMainView() {
        vException.setVisibility(View.GONE);
        mainView.setVisibility(View.VISIBLE);
    }


    public void showExceptionView(int imgSrc, String text, int exceptionType) {
        if (imgSrc > 0) {

        }
        pb.setVisibility(View.GONE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.VISIBLE);
        ivExceptionIcon.setVisibility(View.VISIBLE);
        mainView.setVisibility(View.GONE);

        ivExceptionIcon.setImageResource(imgSrc);
        tvExceptionDescription.setText(text);
        tvExceptionDescription.setTag(exceptionType);
        ivExceptionIcon.setTag(exceptionType);
        tvExceptionDescription.setOnClickListener(this);
        ivExceptionIcon.setOnClickListener(this);
    }

    public void showProgressView() {

        pb.setVisibility(View.VISIBLE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.GONE);
        ivExceptionIcon.setVisibility(View.GONE);
        mainView.setVisibility(View.GONE);
    }


    OnRequestListener<ContentEntity<OrderListBean>> onRequestListener = new OnRequestListener<ContentEntity<OrderListBean>>() {
        @Override
        public ContentEntity<OrderListBean> jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<ContentEntity<OrderListBean>>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            RequestTag requestTag = (RequestTag) tag;
            updateViewStatus(requestTag);
            if (requestSucceedCount == 0) {
                showExceptionView(R.mipmap.order_no, "网络问题,请点击重试", HttpRequest.FAIL_NET_FAULT);
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();

            if (requestSucceedCount == 0) {
                showExceptionView(R.mipmap.order_no, arg + "", HttpRequest.FAIL_DATA_FAULT);//arg + ",请点击重试"
            }
            RequestTag requestTag = (RequestTag) tag;
            updateViewStatus(requestTag);
        }

        @Override
        public void onStart(Object tag) {
            if (requestSucceedCount == 0) {
                showProgressView();
            }
        }

        @Override
        public void onResponse(ResponseEntity<ContentEntity<OrderListBean>> response, Object tag) {
            hideProgressDialog();

            showMainView();
            requestSucceedCount++;
            RequestTag requestTag = (RequestTag) tag;
            if (requestTag.action == 0) {
                list.clear();
            }
            ContentEntity<OrderListBean> orderResponse = response.getResponseElement();
            List<OrderListBean> content = orderResponse.getContent();
            tagPager.setLast(orderResponse.getLast());
            tagPager.setNumber(orderResponse.getNumber());
            tagPager.setSize(orderResponse.getSize());
            tagPager.setTotalElements(orderResponse.getTotalElements());
            list.addAll(content);
            adapter.notifyDataSetChanged();
            if (orderResponse.getNumber() == 0) {
                if (refreshFlag) {
                    lvContent.setSelection(0);
                    refreshFlag = false;
                }
            }
            updateViewStatus(requestTag);
            if (list.size() == 0) {
                showExceptionView(R.mipmap.order_no, "暂无订单", HttpRequest.FAIL_DATA_EMPTY);
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
    }

    public void intoFresh() {
        showProgressView();
        refresh();
    }

    public void startActivityForResultCommon(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 0x001) {
            intoFresh();
        }
    }


    private Dialog dialogCancel;
    TextView[] numbers = new TextView[6];

    void showCodeDialog(String code) {
        if (dialogCancel == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_code, null);
            dialogCancel = new Dialog(context, R.style.ActionSheetDialogStyle);
            dialogCancel.setContentView(view);
            Window dialogWindow = dialogCancel.getWindow();
            if (dialogWindow == null)
                return;
            dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
            dialogWindow.setAttributes(lp);
            numbers[0] = (TextView) view.findViewById(R.id.number1);
            numbers[1] = (TextView) view.findViewById(R.id.number2);
            numbers[2] = (TextView) view.findViewById(R.id.number3);
            numbers[3] = (TextView) view.findViewById(R.id.number4);
            numbers[4] = (TextView) view.findViewById(R.id.number5);
            numbers[5] = (TextView) view.findViewById(R.id.number6);
        }
        for (int i = 0; i < 6; i++) {
            numbers[i].setText("" + code.charAt(i));
        }
        dialogCancel.show();
    }

    private Dialog dialog;
    private int flagPosition;
    View view, v_order_cancel, tv_order_not_want, tv_order_wrong;

    public void showCancelDialog() {
        if (dialog == null) {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_order, null);
            v_order_cancel = view.findViewById(R.id.v_order_cancel);
            tv_order_not_want = view.findViewById(R.id.tv_order_not_want);
            tv_order_wrong = view.findViewById(R.id.tv_order_wrong);
            v_order_cancel.setOnClickListener(this);
            tv_order_wrong.setOnClickListener(this);
            tv_order_not_want.setOnClickListener(this);
            dialog = new Dialog(context, R.style.no_title_dialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();
            if (dialogWindow == null)
                return;
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(30);
            dialogWindow.setAttributes(lp);
        }
        tv_order_wrong.setTag(R.id.item_key_position, flagPosition);
        v_order_cancel.setTag(R.id.item_key_position, flagPosition);
        tv_order_not_want.setTag(R.id.item_key_position, flagPosition);
        dialog.show();
    }

    /**
     * 取消订单
     */
    private void mHttpCancel(String id, String reason) {
        Log.i("列表测试", "" + id + reason);
        RequestEntityPurchase<Map<String, Object>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_order_order_cancel);
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", id);
        map.put("reason", reason);
        data.setParameters(map);
        ((BaseActivity) context).request(data, AppPurchaseRouterConstant.cmd_order_order_cancel, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                Toast.makeText(context, "服务器开小差了……", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                ((BaseActivity) context).toast((arg == null ? "" : arg + ""));
            }

            @Override
            public void onStart(Object tag) {
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                StatusEntity responseElement = response.getResponseElement();
                if (responseElement.isSuccess()) {
                    //requestOrderInfo();
/*                    if(type==0) {
                        OrderAdapter.this.list.get(flagPosition).setButtonType(11);
                        OrderAdapter.this.list.get(flagPosition).setPurchaseStatusTitle("交易关闭");
                    }else{
                        OrderAdapter.this.list.remove(flagPosition);
                    }
                    notifyDataSetChanged();*/
                    intoFresh();
                    T.toast("已取消");
                } else {
                    T.toast("服务器出现小问题，请稍后再试");
                }
            }
        });
    }

    /**
     * 确认收货1 有无尾款
     */
    class SureCheckBean {
        int type;
        double money;
    }

    private void mHttpCheck(final int id) {
        RequestEntityPurchase<Map<String, Object>> dataRequest = new RequestEntityPurchase<>();
        dataRequest.setCmd(AppPurchaseRouterConstant.cmd_order_order_checkReceiveGood);

        Map<String, Object> map = new HashMap<>();
        map.put("orderId", this.list.get(id).getOrderId());
        dataRequest.setParameters(map);
        ((BaseActivity) context).request(dataRequest, AppPurchaseRouterConstant.cmd_order_order_checkReceiveGood, new OnRequestListener<SureCheckBean>() {
            @Override
            public SureCheckBean jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, SureCheckBean.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                Toast.makeText(context, "服务器开小差了……", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                T.toast((arg == null ? "" : arg + ""));
            }

            @Override
            public void onStart(Object tag) {
                ((BaseActivity) context).showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<SureCheckBean> response, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                final SureCheckBean responseElement = response.getResponseElement();
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("本订单已支付，请收到货后再点击“确认收货”！");//设置显示的内容
                if (responseElement.type == 1 || responseElement.type == 2) {//0结束；1：收银台，2：有尾款
                    //builder.setMessage("您已经收到货物，验货通过，清点无误！" + "\n" + "去收银台付款：" + responseElement.money + "元。");
                    builder.setMessage("您还有未支付钱款，去支付？");
                }/* else if (responseElement.type == 2) {
                    builder.setMessage("您已经收到货物，验货通过，清点无误！" + "\n" + "并且已经付清尾款：" + responseElement.money + "元。");
                }*/
                builder.setTitle("")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                if (responseElement.type == 1 || responseElement.type == 2) {
                                    Intent pay = new Intent(context, WXPayEntryActivity.class);
                                    pay.putExtra("orderId", list.get(id).getOrderId());
                                    pay.putExtra("type", "2");
                                    context.startActivity(pay);
                                    return;
                                } else {
                                    mHttpSure(id, responseElement.type);
                                    return;
                                }
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//响应事件
                        // TODO Auto-generated method stub
                    }

                }).show();//在按键响应事件中显示此对话框

            }
        });
    }

    /**
     * 确认收货2
     */
    private void mHttpSure(int id, int type1) {
        RequestEntityPurchase<Map<String, Object>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_order_order_confirmReceiveGood);
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", this.list.get(id).getOrderId());
        map.put("type", "" + type1);
        data.setParameters(map);
        ((BaseActivity) context).request(data, AppPurchaseRouterConstant.cmd_order_order_confirmReceiveGood, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                Toast.makeText(context, "服务器开小差了……", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                T.toast((arg == null ? "" : arg + ""));
            }

            @Override
            public void onStart(Object tag) {
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                StatusEntity responseElement = response.getResponseElement();
                if (responseElement.isSuccess()) {
                    intoFresh();
                } else {
                    T.toast("服务器出现小问题，请稍后再试");
                }
            }
        });
    }

    /**
     * 再来一单
     */
    private void mHttpMore(int id) {
        RequestEntityPurchase<Map<String, Object>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_order_cart_again);
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", list.get(id).getOrderId());
        map.put("accountId", ApplicationHelper.getInstance().getUser().getAccountId());
        map.put("cityId", ApplicationHelper.getInstance().getCity().getCityId());
        data.setParameters(map);
        ((BaseActivity) context).request(data, AppPurchaseRouterConstant.cmd_order_cart_again, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                //Toast.makeText(context, "服务器开小差了……", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                //T.toast((arg == null ? "" : arg + ""));
            }

            @Override
            public void onStart(Object tag) {
                ((BaseActivity) context).showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                ((BaseActivity) context).hideProgressDialog();
                Intent intent = new Intent(context,
                        HomeActivity.class);
                intent.putExtra("home_tab_index", 1);
                DataManager.getInstance().isNeedRefreshCart = true;
                context.startActivity(intent);
                adapter.notifyDataSetChanged();
            }
        });
    }

    long mLastOnClickTime;

    public boolean isWindowLocked() {
        long current = SystemClock.elapsedRealtime();
        if (current - this.mLastOnClickTime > 1500L) {
            this.mLastOnClickTime = current;
            return false;
        } else {
            return true;
        }
    }


}